package org.usts.trainning.supermarket.service;

import java.text.ParseException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.usts.trainning.supermarket.dao.BillDao;
import org.usts.trainning.supermarket.entity.Bill;

public class BillContribute {
	BillDao dao = new BillDao();

	public String getAllBill(HttpServletRequest request, HttpServletResponse response) {
		List<Bill> list = dao.findAllBill();
		dao.close();
		// 代码（1）保存处理结果并返回跳转页面
		request.setAttribute("listallbill", list);
		return "/billList.jsp";
	}

	public String findBillById(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("bid");
		int bid = Integer.parseInt(id);
		Bill bill = dao.findBillById(bid);
		dao.close();
		// 代码（1）保存处理结果并返回跳转页面
		request.setAttribute("lookbill", bill);
		return "/billView.jsp";
	}

	public String updateBillById(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("bid");
		int bid = Integer.parseInt(id);
		Bill bill = dao.findBillById(bid);
		dao.close();
		// 代码（1）保存处理结果并返回跳转页面
		request.setAttribute("updatebill", bill);
		return "/billUpdate.jsp";
	}

	public String modifyBillById(HttpServletRequest request, HttpServletResponse response) {
		Bill bill = new Bill();
		bill.setBid(Integer.parseInt(request.getParameter("bid")));
		bill.setBcode(request.getParameter("bcode"));
		bill.setBname(request.getParameter("bname"));
		bill.setPsupplier(request.getParameter("psupplier"));
		bill.setBcount(Integer.parseInt(request.getParameter("bcount")));
		bill.setBmoney(Double.parseDouble(request.getParameter("bmoney")));
		bill.setBflag(request.getParameter("bflag"));
		bill.setBtime(request.getParameter("btime"));
		dao.updateBillById(bill);
		getAllBill(request, response);
		dao.close();
		// 代码（1）保存处理结果并返回跳转页面

		request.setAttribute("modifybill", bill);
		return "/billList.jsp";
	}

	public String deleteBillById(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("bid");
		int bid = Integer.parseInt(id);
		boolean flag = dao.deleteUser(bid);
		getAllBill(request, response);
		dao.close();
		// 代码（1）保存处理结果并返回跳转页面
		return "/billList.jsp";

	}

	public String addBill(HttpServletRequest request, HttpServletResponse response) {
		Bill bill = new Bill();
		bill.setBcode(request.getParameter("bcode"));
		bill.setBname(request.getParameter("bname"));
		bill.setPsupplier(request.getParameter("psupplier"));
		bill.setBcount(Integer.parseInt(request.getParameter("bcount")));
		bill.setBmoney(Double.parseDouble(request.getParameter("bmoney")));
		bill.setBflag(request.getParameter("bflag"));
		bill.setBtime(request.getParameter("btime"));
		boolean falge = dao.addBill(bill);
		getAllBill(request, response);
		dao.close();
		// 代码（1）保存处理结果并返回跳转页面
		return "/billList.jsp";
	}

	public String searchBill(HttpServletRequest request, HttpServletResponse response) {
		Bill bill = new Bill();
		bill.setBname(request.getParameter("bname"));
		bill.setPsupplier(request.getParameter("psupplier"));
		bill.setBflag(request.getParameter("bflag"));
		List<Bill> list = dao.searchBill(bill);
		dao.close();
		// 代码（1）保存处理结果并返回跳转页面
		request.setAttribute("listallbill", list);
		return "/billList.jsp";

	}

}
